package com.hui.server.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hui.server.config.redis.RedisCacheConfig;
import com.hui.server.entity.Menu;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * Mapper 接口
 * </p>
 * 开启缓存，默认所有查询都缓存，所有修改删除新增清空缓存
 *
 * @author hui
 * 有关于mybatis的注解式开发 ：   https://blog.csdn.net/zhoushimiao1990/article/details/99944357?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=1
 * @since 2022-03-31
 */


@CacheNamespace(implementation = RedisCacheConfig.class)
public interface MenuMapper extends BaseMapper<Menu> {

    /**
     * 基本的返回结果，相当于xml里的通用 resultMap 查询
     * <br/>
     * 查询全部字段
     *
     * @param id
     * @return
     */
    @Select("select `id`,`url`,`path`,`component`,`name`,`iconCls`,`keepAlive`,`requireAuth`,`requireAuth`,`parentId`,`enabled` from t_menu where id =#{id}")
    @Results(
            id = "baseResultMap",
            value = {
                    @Result(id = true, property = "id", column = "id"),
                    @Result(property = "url", column = "url"),
                    @Result(property = "path", column = "path"),
                    @Result(property = "component", column = "component"),
                    @Result(property = "name", column = "name"),
                    @Result(property = "iconCls", column = "iconCls"),
                    @Result(property = "keepAlive", column = "keepAlive"),
                    @Result(property = "requireAuth", column = "requireAuth"),
                    @Result(property = "parentId", column = "parentId"),
                    @Result(property = "enabled", column = "enabled")
            }
    )
    Menu getMenuById(@Param("id") Integer id);


    /**
     * 通过用户ID查询菜单列表<p/
     * 先按照用户名查所需要的parent，再从parent的id传给children的parentId
     * <br/>
     * <br/>
     * 两个注解@Results与@ResultMap不能一起使用，就是除了 baseResultMap 的字段之外不能再添加而外的字段，也许是bug，也可能是技术菜
     *
     * @param id
     * @return
     */
    @Select("select `id`,`url`,`path`,`component`,`name`,`iconCls`,`keepAlive`,`requireAuth`,`requireAuth`,`parentId`,`enabled` " +
            "from t_menu where id in(\n" +
            "\tselect\n" +
            "\tdistinct m1.parentId\n" +
            "from\n" +
            "\n" +
            "t_menu m1,\n" +
            "t_menu_role mr,\n" +
            "t_admin_role ar\n" +
            " \n" +
            "\n" +
            "WHERE \n" +
            "mr.mid =m1.id\n" +
            "and\n" +
            "mr.rid =ar.rid\n" +
            "and\n" +
            "m1.enabled=true\n" +
            "and\n" +
            "ar.adminId =#{id}\n" +
            ") ")
    @Results(
            value = {
                    @Result(id = true, property = "id", column = "id"),
                    @Result(property = "children", many = @Many(select = "getChildrenMenusByParentId"), column = "id")
            }
    )
    List<Menu> getMenusByAdminId(@Param("id") Integer id);


    /**
     * 根据参数id获取parent，多表查询的子查询
     *
     * @param id
     * @return
     */
    @Select("select `id`,`url`,`path`,`component`,`name`,`iconCls`,`keepAlive`,`requireAuth`,`requireAuth`,`parentId`,`enabled` from t_menu where parentId =#{id}")
    @ResultMap("baseResultMap")
    List<Menu> getChildrenMenusByParentId(@Param("id") Integer id);


    /**
     * 查询所有角色拥有的所有菜单
     * <br/>
     * <p>先根据角色的id查询menu，再多表查询根据menuId查询role<p/>
     *
     * @return
     */
    @Select("select m.*\n" +
            "from \n" +
            "t_menu m,\n" +
            "t_menu_role mr,\n" +
            "t_role r\n" +
            "where \n" +
            "m.id =mr.mid\n" +
            "and \n" +
            "mr.rid =r.id\n" +
            "order by m.id;")
    @Results(
            value = {
                    @Result(id = true, property = "id", column = "id"),
                    @Result(property = "roles", many = @Many(select = "com.hui.server.mapper.RoleMapper.getRoleByMenuId"), column = "id")
            }
    )
    List<Menu> getMenusByRoles();


    /**
     * 查询全部菜单， 查出对应父子关系，循环递归查询
     * @return
     */
    @Select("select \n" +
            "id , `name`\n" +
            "from \n" +
            "t_menu\n" +
            "where\n" +
            "id =1")
    @Results(
            value = {
                    @Result(id = true, property = "id", column = "id"),
                    @Result(property = "children", many = @Many(select = "getAllMenusChildren"), column = "id")
            }
    )
    List<Menu> getAllMenus();


    @Select("select \n" +
            "id , `name`\n" +
            "from \n" +
            "t_menu\n" +
            "where\n" +
            "parentId =#{parentId}")
    @Results(
            value = {
                    @Result(id = true, property = "id", column = "id"),
                    @Result(property = "children", many = @Many(select = "getAllMenusChildren"), column = "id")
            }
    )
    List<Menu> getAllMenusChildren(@Param("parentId") Integer parentId);
}
